package com.zhenmaitang.clinic_sys.util;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

public class SecurityUtils {
    
    public static Integer getCurrentUserId() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() instanceof UserDetails) {
            UserDetails userDetails = (UserDetails) authentication.getPrincipal();
            // 这里假设用户名就是用户ID，实际项目中可能需要从UserDetails获取更多信息
            try {
                return Integer.parseInt(userDetails.getUsername());
            } catch (NumberFormatException e) {
                return 1; // 默认用户ID，仅用于测试
            }
        }
        return 1; // 默认用户ID，仅用于测试
    }
}